import { _decorator, Component, Button, Sprite, director, sys } from 'cc';
const { ccclass, property } = _decorator

import { SS_AudioManager } from '../Manager/SS_AudioManager';
import { SS_Settings } from '../Data/SS_Settings';

@ccclass('SettingPanel')
export default class SettingPanel extends Component {
    static Instance: SettingPanel;
    @property(Button)
    Button_close: Button | null = null
    @property(Button)
    Button_back: Button | null = null
    @property(Button)
    Button_music: Button | null = null;
    @property(Sprite)
    music_on: Sprite | null = null;
    @property(Sprite)
    music_off: Sprite | null = null;
    @property(Button)
    Button_sound: Button | null = null;
    @property(Sprite)
    sound_on: Sprite | null = null;
    @property(Sprite)
    sound_off: Sprite | null = null;
    curMusicV
    curEffectV

    protected onLoad(): void {
        SettingPanel.Instance = this;

        this.Init();
        this.AddListener();
        this.Refresh();
    }
    Init() {

    }
    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh();
    }
    ShowOff() {
        this.node.active = false;
        this.RemoveListener();
    }
    AddListener() {
        this.Button_close.node.on('click', this.on_Button_close, this)
        this.Button_back.node.on('click', this.on_Button_back, this)
        this.Button_music.node.on('click', this.On_Button_music, this);
        this.Button_sound.node.on('click', this.On_Button_sound, this);
    }
    RemoveListener() {
        this.Button_close.node.off('click', this.on_Button_close, this)
        this.Button_back.node.off('click', this.on_Button_back, this)
        this.Button_music.node.off('click', this.On_Button_music, this);
        this.Button_sound.node.off('click', this.On_Button_sound, this);
    }
    on_Button_close() {
        this.ShowOff();

        SS_AudioManager.PlayButtonAudio();
    }
    on_Button_back() {
        this.ShowOff();
        director.loadScene("MainScene");

        SS_AudioManager.PlayButtonAudio();
    }
    
    On_Button_music() {
        if (SS_Settings.curMusicV == 0) {
            SS_Settings.curMusicV = 1;

            SS_AudioManager.Instance.AudioSource_bgm.volume = SS_Settings.curMusicV;
            SS_AudioManager.Instance.AudioSource_effect.volume = SS_Settings.curMusicV;
        } else {
            SS_Settings.curMusicV = 0;

            SS_AudioManager.Instance.AudioSource_bgm.volume = SS_Settings.curMusicV;
            SS_AudioManager.Instance.AudioSource_effect.volume = SS_Settings.curMusicV;
        }
        SS_Settings.saveVolumeData();


        this.Refresh_music();

        SS_AudioManager.PlayButtonAudio();
    }

    On_Button_sound() {
        if (SS_Settings.curVibrate == 0) {
            SS_Settings.curVibrate = 1;
        } else {
            SS_Settings.curVibrate = 0;
        }
        SS_Settings.saveVolumeData();

        this.Refresh_sound();

        SS_AudioManager.PlayButtonAudio();
    }
    Refresh() {
        this.Refresh_music();
        this.Refresh_sound();
    }
    Refresh_music() {
        this.music_on.node.active = SS_Settings.curMusicV === 1;
        this.music_off.node.active = !(SS_Settings.curMusicV === 1);
    }
    Refresh_sound() {
        this.sound_on.node.active = SS_Settings.curVibrate === 1;
        this.sound_off.node.active = !(SS_Settings.curVibrate === 1);
    }
}



/** 调试 */
window["SettingPanel"] = SettingPanel;